<?php 
    header("Content-type: text/html; charset=utf-8");
    require_once('auth_i.inc.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=8" />
    <link rel="stylesheet" type="text/css" media="screen" href="css/themes/redmond/jquery-ui-1.8.9.custom.css" />
    <link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />
    <link rel="stylesheet" type="text/css" media="screen" href="css/ui.multiselect.css" />
    <link rel="stylesheet" type="text/css" media="screen" href="css/asud.css" />

    <script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
    <script src="js/i18n/grid.locale-ru.js" type="text/javascript"></script>
    <script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>
    <script src="js/jquery-ui-1.8.9.custom.min.js" type="text/javascript"></script>

<script type="text/javascript">

$(function() { 
  $("#list").jqGrid({
    url:'req/grp_list.php',
    editurl:'req/grp_edit.php', 
    datatype: 'json',
    width: 500,
    mtype: 'GET',
    colNames:['Название', 'Описание'],
    colModel :[ 
      {name:'name', index:'name', width:185, editable: true, editoptions:{size:20} }, 
      {name:'g_desc', index:'g_desc', width:200, editable: true, edittype:"textarea", editoptions:{rows:"2",cols:"20"}}, 
    ],
    sortname: 'name',
    sortorder: 'desc',
    viewrecords: true,
  });

function checkSubmit(data, formid)
{
    if (data.name == "")
    {
        return [false, "Название группы не может быть пустым !"]
    }

    if (data.g_desc == "")
    {
        return [false, "Описание группы не может быть пустым !"]
    }

    return [true,""]
}

$( "#edit-button" )
            .button()
            .click(function() {
                var gr = jQuery("#list").jqGrid('getGridParam','selrow'); 

                if( gr != null ) 
                    jQuery("#list").jqGrid('editGridRow',gr, { reloadAfterSubmit:false,closeAfterEdit:true,
                        modal:true,closeOnEscape:true, beforeSubmit: checkSubmit }); 

                else 
                    alert("Выбирите строку для редактирования !"); 
            });

$( "#add-button" )
            .button()
            .click(function() {
                    jQuery("#list").jqGrid('editGridRow',"new",{reloadAfterSubmit:true,closeAfterAdd:true,
                                        modal:true,closeOnEscape:true, beforeSubmit: checkSubmit }); 
            });


$("#remove-button")
            .button()
            .click(function() { 

                var gr = $("#list").jqGrid('getGridParam','selrow'); 
                if( gr != null ) 
                {
                    $("#list").jqGrid('delGridRow',gr,{reloadAfterSubmit:true,
                        afterSubmit: function(response, postdata)
                        {
                            if (response.status == 200 && response.responseText == "")
                            {
                                return [true,""]
                            }
                            else
                            {
                                var resp = $.parseJSON(response.responseText);
                                alert(resp.error);
                                return [true, ""];
                            }
                        }
                    }); 
                }
                else 
                {
                    alert("Выбирите строку для удаления !"); 
                }
            });
        
}); 

</script>

</head>
<body>
<?php // menu bar
    require_once('menu.inc.php');

    print menu($menu_items, 'group.php');
?>

<div id="main-box">
    <table id="list"></table> 
    <div id="button-box">   
        <button id="add-button">Добавить</button>
        <button id="edit-button">Редактировать</button>
        <button id="remove-button">Удалить</button>
    </div>
</div>

</body>
</html>
